package com.hz.us.service.impl;

import com.hz.api.model.dto.StudentDTO;
import com.hz.us.mapper.ClassMapper;
import com.hz.us.mapper.GradeMapper;
import com.hz.us.mapper.UserMapper;
import com.hz.us.model.entity.ClassEntity;
import com.hz.us.model.entity.GradeEntity;
import com.hz.us.model.entity.UserEntity;
import com.hz.us.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    UserMapper userMapper;

    @Resource
    GradeMapper gradeMapper;

    @Resource
    ClassMapper classMapper;

    @Override
    public StudentDTO getStudentById(Integer studentId) {
        UserEntity userEntity = userMapper.selectStudentById(studentId);
        if (userEntity == null) {
            return null;
        }
        StudentDTO student = new StudentDTO();
        student.setId(userEntity.getId());
        student.setUsername(userEntity.getUsername());
        student.setRealname(userEntity.getRealname());
        student.setStudentId(userEntity.getStudent_id());
        student.setGradeId(userEntity.getGrade_id());
        GradeEntity gradeEntity = gradeMapper.selectById(userEntity.getGrade_id());
        student.setGradeName(gradeEntity.getGrade_name());
        student.setGradeYear(gradeEntity.getGrade_year());
        ClassEntity classEntity = classMapper.selectById(userEntity.getClass_id());
        student.setClassId(classEntity.getId());
        student.setClassName(classEntity.getClass_name());
        return student;
    }
}
